// Created by WXX on 2022/1/23 21:22
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>

using namespace std;

/**
 * 执行用时：524 ms, 在所有 C++ 提交中击败了18.52%的用户
 * 内存消耗：160.2 MB, 在所有 C++ 提交中击败了66.17%的用户
 */
class StockPrice {
public:

    unordered_map<int, int> tp;  // (time, price)
    map<int, int> ps;  // 有效价格出现的次数
    int last_time = 0, last_price = 0;
    int minv = 2e9, maxv = 0;  // 股票最低价、最高价

    StockPrice() {

    }

    void update(int timestamp, int price) {

        if (tp.count(timestamp)) {
            int t = tp[timestamp];
            ps[t]--;
            if (ps[t] == 0) ps.erase(t);
        }
        tp[timestamp] = price;
        ps[price]++;
        maxv = ps.rbegin()->first;
        minv = ps.begin()->first;

        if (timestamp >= last_time) {
            last_time = timestamp;
            last_price = price;
        }
    }

    int current() {
        return last_price;
    }

    int maximum() {
        return maxv;
    }

    int minimum() {
        return minv;
    }
};

int main() {

    StockPrice *t = new StockPrice();
    t->update(1, 10);
    t->update(2, 5);
    cout << t->current() << endl;  // 5
    cout << t->maximum() << endl;  // 10
    t->update(1, 3);
    cout << t->maximum() << endl;  // 5
    t->update(4, 2);
    cout << t->minimum() << endl;  // 2

    return 0;
}
